package top.lyc829.zzl_android.ui.index;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.logic.bean.BaseBean;

public class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.ViewHolder> {

    private Context mContext;
    private List<BaseBean> mBaseList;

    public BaseAdapter(List<BaseBean> mBaseList) {
        this.mBaseList = mBaseList;
    }

    public void setmBaseList(List<BaseBean> mBaseList) {
        this.mBaseList = mBaseList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.index_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BaseBean base = mBaseList.get(position);
        holder.baseIndexTitle.setText(base.getTitle());
        Glide.with(mContext)
                .load(base.getIndexImgUrl())
                .into(holder.baseIndexImage);
    }


    @Override
    public int getItemCount() {
        return mBaseList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        ImageView baseIndexImage;
        TextView baseIndexTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = (CardView) itemView;
            baseIndexImage = itemView.findViewById(R.id.base_index_image);
            baseIndexTitle = itemView.findViewById(R.id.base_index_title);
        }
    }
}
